package com.nail.api.customMake.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.nail.api.customMake.bean.FileUploadInfo;
import com.nail.api.customMake.dao.IFileUploadDao;
import com.nail.api.user.bean.User;
import com.nail.utils.PageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nail.api.customMake.bean.Indent;
import com.nail.api.customMake.bean.Template;
import com.nail.api.customMake.dao.IIndentDao;
import com.nail.api.customMake.dao.ITemplateDao;
import com.nail.api.customMake.service.ITemplateService;

import java.util.List;

@Service("templateService")
public class TemplateServiceImpl implements ITemplateService {
	Logger logger = Logger.getLogger(this.getClass());
	
    @Autowired
    private ITemplateDao templateDao;

    @Autowired
    private IFileUploadDao fileUploadDao;

    @Transactional
    public void addTemplate(Template template){
    	templateDao.insertSelective(template);
    }

    @Transactional
    public void deleteTemplate(Long templateId){
    	templateDao.deleteByPrimaryKey(templateId);
    }

    @Transactional
    public void updateTemplate(Template template){
    	templateDao.updateByPrimaryKeySelective(template);
    }

    public PageResult getTemplate(PageResult pageResult, String templateType){
        List<Template> list;
        Integer num;
        if(templateType == null){
            list = templateDao.getTemplateAll(pageResult.getBegin(),pageResult.getPageSize());
            num = templateDao.getTemplateCountAll();
        }else{
            list = templateDao.getTemplate(templateType,pageResult.getBegin(),pageResult.getPageSize());
            num = templateDao.getTemplateCount(templateType);
        }
        pageResult.setData(list);
        pageResult.setTotalNum(num);
        return pageResult;
    }

    public Template getTemplateById(User user, Long id){
        Template template = null;
        if(user.getRole().getRoleType().equals(1)){
             template =templateDao.selectByPrimaryKey(id);
        }else{
             template =templateDao.getTemplateByIdAndUserId(user.getId(),id);
        }

        if(template != null && !StringUtils.isEmpty(template.getImgIds())){
            List<FileUploadInfo> fileUploadInfos = fileUploadDao.getFileUploadInfoByIds(template.getImgIds());
            template.setImgs(fileUploadInfos);
        }
        return template;
    }

    @Transactional
    public void setSort(Long id,Integer sort){
        templateDao.setSort(id,sort);
    }

    @Transactional
    public void setIsAble(Long id,Integer isAble){
        templateDao.setIsAble(id,isAble);
    }
}
